package cn.itcast.service.impl;

import cn.itcast.dao.AreaDao;
import cn.itcast.domain.Area;
import cn.itcast.service.AreaService;
import cn.itcast.utils.PinYin4jUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.beans.Transient;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaDao areaDao;

    @Override
    @Transient
    public void upload(MultipartFile file) throws IOException {
        List<Area> areas = new ArrayList<>();
        // 编写解析代码逻辑
        // 基于.xls 格式解析 HSSF
        // 1、 加载Excel文件对象

        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(file.getInputStream());
        // 2、 读取一个sheet
        HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
        // 3、 读取sheet中每一行
        for (Row row : sheet) {
            // 一行数据 对应 一个区域对象
            if (row.getRowNum() == 0) {
                // 第一行 跳过
                continue;
            }
            // 跳过空行
            if (row.getCell(0) == null
                    || StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
                continue;
            }
            Area area = new Area();
            area.setId(row.getCell(0).getStringCellValue());
            area.setProvince(row.getCell(1).getStringCellValue());
            area.setCity(row.getCell(2).getStringCellValue());
            area.setDistrict(row.getCell(3).getStringCellValue());
            area.setPostcode(row.getCell(4).getStringCellValue());

            //城市编码
            area.setCitycode(PinYin4jUtils.hanziToPinyin(area.getCity().substring(0,area.getCity().length()-1),""));
            //简码
            String jm = area.getProvince().substring(0,area.getProvince().length()-1) + area.getCity().substring(0,area.getCity().length()-1) + area.getDistrict().substring(0,area.getDistrict().length()-1);
            //System.out.println(jm);
            String[] headByString = PinYin4jUtils.getHeadByString(jm);
            area.setShortcode(PinYin4jUtils.stringArrayToString(headByString));
            areas.add(area);
        }
        areaDao.saveAll(areas);
    }

    /**
     * 分页查询
     * @param page
     * @param rows
     * @return
     */
    @Override
    @Transactional
    public Page<Area> pageQuery(int page, int rows) {
        PageRequest pageRequest = PageRequest.of(page-1, rows);
        Page<Area> areas = areaDao.findAll(pageRequest);
        return areas;
    }

    /**
     * 分页动态查询
     * @param area
     * @param page
     * @param rows
     * @return
     */
    @Override
    @Transactional
    public Page<Area> pageQuery(Area area, int page, int rows) {

        Specification<Area> specification = new Specification<Area>() {
            @Override
            public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                ArrayList<Predicate> list = new ArrayList<>();
                if (area.getProvince()!=null&&!"".equals(area.getProvince())){
                    list.add(cb.like(root.get("province"),"%"+area.getProvince()+"%"));
                }
                if (area.getCity()!=null&&!"".equals(area.getCity())){
                    list.add(cb.like(root.get("city"),"%"+area.getCity()+"%"));
                }
                if (area.getDistrict()!=null&&!"".equals(area.getDistrict())){
                    list.add(cb.like(root.get("district"),"%"+area.getDistrict()+"%"));
                }

                return cb.and(list.toArray(new Predicate[0]));
            }
        };
        PageRequest request = PageRequest.of(page - 1, rows);
        Page<Area> areaDaoAll = areaDao.findAll(specification, request);
        return areaDaoAll;
    }

}
